﻿using System.Collections;
using System.Reflection;
using UnityEngine;

class ConfigAssetMgr : Singleton<ConfigAssetMgr>
{
    private WWW www = null;
    private ABDepends abDepends;
    private AssetBundle scriptAB;
    private AssetBundle shaderAB;
    private Assembly assembly;

    private int totalLoadCount = 4;
    private int loadedCount;

    private FileData fileData = new FileData();
    public bool IsLoaded { get; private set; }
    public bool IsLoading { get; private set; }

    public float LoadProgress
    {
        get { return (loadedCount + www.progress) * (1f / totalLoadCount); }
    }
    
    /// <summary>
    /// 加载依赖配置
    /// </summary>
    public IEnumerator RunLoadConfig()
    {
        IsLoaded = false; IsLoading = true;
        //加载资源依赖配置
        www = new WWW(PathUtil.CheckPath(PathUtil.ABDEPENDS_PATH));
        yield return www;
        var ab = www.assetBundle;
        abDepends = ab.mainAsset as ABDepends;
        abDepends.Init();

        ab.Unload(true);
        www.Dispose();
        loadedCount++;

        //加载引用脚本
        www = new WWW(PathUtil.CheckPath(PathUtil.SCRIPT_PATH));
        yield return www;
        scriptAB = www.assetBundle;
        www.Dispose();
        loadedCount++;

        //加载引用脚本
        www = new WWW(PathUtil.CheckPath(PathUtil.SHADER_PATH));
        yield return www;
        shaderAB = www.assetBundle;
        www.Dispose();
        loadedCount++;

        //加载所有json文件
        www = new WWW(PathUtil.CheckPath(PathUtil.CONFIG_PATH));
        yield return www;
        ab = www.assetBundle;
        DBSystemMgr.GetSingleton().InitAllCfg(ab.LoadAllAssets());
        ab.Unload(true);
        www.Dispose();

        if (Utility.GetPlatForm() == EPlatform.Editor)
        {
            www = new WWW(PathUtil.CheckPath(PathUtil.VS_CONFIG));
            yield return www;
            fileData.ReadBytes(www.bytes);
        }
        IsLoading = false; IsLoaded = true;
        ZEventCenter.DispatchEvent(new ZEvent(ConstData.LOAD_CONFIG_COMPLETE));
    }

    /// <summary>
    /// 加载热更新dll
    /// </summary>
    public IEnumerator LoadAssembly()
    {
        WWW www = new WWW(PathUtil.CheckPath(PathUtil.DLL_PATH));
        yield return www;
        AssetBundle ab = www.assetBundle;
        www.Dispose();

        TextAsset ts = ab.LoadAsset("hotcode.dll", typeof(TextAsset)) as TextAsset;
        assembly = Assembly.Load(ts.bytes);
        ab.Unload(false);
        yield break;
    }

    /// <summary>
    /// 加载lua文件
    /// </summary>
    public IEnumerator RunLoadLua()
    {
        WWW www = new WWW(PathUtil.CheckPath(PathUtil.LUA_PATH));
        yield return www;
        AssetBundle ab = www.assetBundle;
        www.Dispose();

        LuaData data = ab.mainAsset as LuaData;
        LuaMgr.GetSingleton().InitData(data);
        ZEventCenter.DispatchEvent(new ZEvent(ConstData.LOAD_LUA_COMPLETE));

        ab.Unload(false);
        yield break;
    }

    public bool GetDepends(string key, out string[] depends)
    {
        depends = abDepends.GetDepends(key);
        return depends != null;
    }

    public bool HasAsset(string path)
    {
        return fileData.files.Contains(path);
    }
}